package com.tianqi.common.utils;

import com.alibaba.fastjson2.JSON;
import com.github.kevinsawicki.http.HttpRequest;
import com.tianqi.modules.pms.dto.response.GeocodeRespDTO;
import com.tianqi.modules.pms.dto.response.TimeConvertResultDTO;
import com.tianqi.modules.tqs.dto.QuDataDTO;
import com.tianqi.modules.tqs.dto.ShengDataDTO;
import com.tianqi.modules.tqs.dto.ShiDataDTO;
import com.tianqi.modules.tqs.model.TimeConvertInfoData;
import com.tianqi.modules.tqs.service.TimeConvertInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @className: TimeUtils
 * @description: 时间工具类
 * @author: zp
 * @create: 2023-08-07 15:43
 **/
@Slf4j
@Service
public class TimeUtils {
    @Value("${json.area}")
    private String cityJson;
    @Autowired
    private TimeConvertInfoService timeConvertInfoService;
    /**
     * @description: 根据经度计算与北京时间时间差，并转换成地方时
     * @author: zp
     * @date: 2023/8/7 20:35
     * @param: [stime, currentLon]
     * @return: java.util.Date
     **/
    public TimeConvertResultDTO getTimeConvert(Date stime, String addr){
        TimeConvertResultDTO resultData=new TimeConvertResultDTO();
        Date resultDate=stime;
        TimeConvertInfoData timeConvertInfoData=new TimeConvertInfoData()
                .setAddr(addr);
        TimeConvertInfoData zhenshiData=timeConvertInfoService.timeConvertInfoQueryOne(timeConvertInfoData);
        if(zhenshiData.getId()==null){
            Map data = new HashMap();
            data.put("address", addr);
            data.put("output", "JSON");
            data.put("key", "41799fe8b0a5ebcf1e31b9badce75640");
            String result = HttpRequest.get("https://restapi.amap.com/v3/geocode/geo").form(data).body();
            log.info("result:" + result);
            GeocodeRespDTO jsonObj = JSON.parseObject(result, GeocodeRespDTO.class);
            if (jsonObj.getInfo().equals("OK")) {
                String location = jsonObj.getGeocodes().get(0).getLocation();
                resultData.setLocation(location);
                String currentLon =location.split(",")[0];

                Double beijingLat = Double.valueOf("120");
                Double latD =beijingLat-Double.valueOf(currentLon);
                if(latD>180){
                    latD=-(360-latD);
                }
                if(latD<-180){
                    latD=-(360+latD);
                }
                int timeD =0;
                if(latD<0){
                     timeD=(int)(latD*4*60*1000);
                    resultDate=new Date(stime.getTime()+timeD);
                }
                if(latD>0){
                     timeD=(int)(latD*4*60*1000);
                    resultDate=new Date(stime.getTime()-timeD);
                }
                TimeConvertInfoData newData=new TimeConvertInfoData()
                        .setAddr(addr)
                        .setLat(currentLon)
                        .setLon(location.split(",")[1])
                        .setTimeD(timeD)
                        .setCreateTime(new Date())
                        .setUpdateTime(new Date());
                timeConvertInfoService.timeConvertInfoSave(newData);
            }

        }else{
            if(zhenshiData.getTimeD()<0){
                resultDate=new Date(stime.getTime()+zhenshiData.getTimeD());
            }
            if(zhenshiData.getTimeD()>0){
                resultDate=new Date(stime.getTime()-zhenshiData.getTimeD());
            }
            resultData.setLocation(zhenshiData.getLat()+","+zhenshiData.getLon());
        }

        resultData.setDate(resultDate);
        return resultData;

    }
    public void TimeConvertShengShi(){
        log.info(cityJson);
        String jsonStr = null;
        try {
            InputStream inputStream = this.getClass().getResourceAsStream(cityJson);
            InputStreamReader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
            int ch = 0;
            StringBuilder sb = new StringBuilder();
            while ((ch = reader.read()) != -1) {
                sb.append((char) ch);
            }
            reader.close();
            jsonStr = sb.toString();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        List<ShengDataDTO> studentList  = JSON.parseArray(jsonStr, ShengDataDTO.class);
        studentList.forEach((e)->{
            getTimeConvert(new Date(),e.getLabel());
            List<ShiDataDTO> shiDataDTO = e.getChildren();
            shiDataDTO.forEach((a)->{
                getTimeConvert(new Date(),e.getLabel()+a.getLabel());
                List<QuDataDTO> squDataDTO = a.getChildren();
                squDataDTO.forEach((b)->{
                    getTimeConvert(new Date(),e.getLabel()+a.getLabel()+b.getLabel());
                });

            });
        });

    }

}
